package metro.jme.lottery.main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.*;
import metro.jme.lottery.graphics.screens.AboutScreen;
import metro.jme.lottery.graphics.screens.EditFavScreen;
import metro.jme.lottery.graphics.screens.HelpScreen;
import metro.jme.lottery.graphics.screens.MainCanvas;
import metro.jme.lottery.graphics.screens.NumberCanvas;
import metro.jme.lottery.graphics.screens.TrackSelecter;
import metro.jme.lottery.sound.MidiPlayer;

/**
 * Main class of the application.
 * @author pierre
 */
public class LotteryMIDlet extends MIDlet implements CommandListener {

	// Different commandes used
	private Command		m_exitCmd;
	private Command		m_checkCmd;
	private Command		m_editFavCmd;
	private Command		m_editAcceptedCmd;
	private Command		m_backCmd;
	private Command		m_helpCmd;
	private Command		m_aboutCmd;
	private Command		m_playCmd;
	private Command		m_pauseCmd;
	private Command		m_selectCmd;
	private Display		m_display;
	private MainCanvas	m_mainScreen;
	
	// Runnable class
	private NumberCanvas	nc;
	private MidiPlayer		m_player;

	private static boolean	INTERNET_CONNECTION_ACCEPTED = false;
	
	/**
	 * Default Constructor
	 */
	public LotteryMIDlet() {

		// Instanciate all used commands
		m_exitCmd			= new Command("Exit", Command.EXIT, 1);
		m_checkCmd			= new Command("Check", Command.ITEM, 2);
		m_editFavCmd		= new Command("Edit", Command.ITEM, 2);
		m_helpCmd			= new Command("Help", Command.ITEM, 2);
		m_aboutCmd			= new Command("About", Command.ITEM, 2);
		m_playCmd			= new Command("Play", Command.ITEM, 2);
		m_pauseCmd			= new Command("Pause", Command.ITEM, 2);
		m_selectCmd			= new Command("Select", Command.ITEM, 2);
		m_backCmd			= new Command("Back", Command.BACK, 1);
		m_editAcceptedCmd	= new Command("Accept", Command.SCREEN, 1);
		System.out.println("Hello world !");
		m_player = new MidiPlayer(this);

	}

	/**
	 * MIDlet method
	 */
	public void startApp() {
		if (m_display == null) {
			m_display = Display.getDisplay(this);
		}

		m_mainScreen = new MainCanvas();
		System.out.println("Width of the current device : " + m_mainScreen.getWidth());

		// All all commands to the screen
		m_mainScreen.addCommand(m_exitCmd);
		m_mainScreen.addCommand(m_checkCmd);
		m_mainScreen.addCommand(m_editFavCmd);
		m_mainScreen.addCommand(m_helpCmd);
		m_mainScreen.addCommand(m_selectCmd);
		m_mainScreen.addCommand(m_aboutCmd);
		if (m_player.isRunning()) {
			m_mainScreen.addCommand(m_pauseCmd);
		} else {
			m_mainScreen.addCommand(m_playCmd);
		}

		m_mainScreen.setCommandListener(this);
		// Display the form
		m_display.setCurrent(m_mainScreen);

	}

	/**
	 * MIDlet method
	 */
	public void pauseApp() {
		stopThreads();
		startApp();
	}

	/**
	 * MIDlet method
	 */
	public void destroyApp(boolean unconditional) {
		notifyDestroyed();
	}

	/**
	 * CommandListener method
	 */
	public void commandAction(Command cmnd, Displayable dsplbl) {
		if (cmnd == m_exitCmd) {
			destroyApp(true);
		} else if (cmnd == m_editFavCmd) {
			// Load new view
			EditFavScreen efv = new EditFavScreen("Edit your saved number sets", this);
			m_display.setCurrent(efv);
			efv.setCommandListener(efv);
		} else if (cmnd == m_checkCmd) {
			// Load new view
			if(!INTERNET_CONNECTION_ACCEPTED) {
				Alert al = null;
				al = new Alert("Connection alert", "You will be connected to internet", null, AlertType.WARNING);
				al.setTimeout(Alert.FOREVER);

				al.addCommand(m_backCmd);
				al.addCommand(m_editAcceptedCmd);
				al.setCommandListener(this);
				m_display.setCurrent(al);
			} else {
				commandAction(m_editAcceptedCmd, dsplbl);
			}
		} else if (cmnd == m_editAcceptedCmd) {
			if(!INTERNET_CONNECTION_ACCEPTED) {
				INTERNET_CONNECTION_ACCEPTED = true;
			}
			try {
				nc = new NumberCanvas(this);
				m_display.setCurrent(nc);
				nc.start();
				nc.setCommandListener(nc);
			} catch (Exception e) {
				// Display an alert
				Alert al = new Alert("Connection alert", "Application can not find any internet connection", null, AlertType.WARNING);
				al.setTimeout(Alert.FOREVER);
				al.addCommand(m_backCmd);
				al.setCommandListener(this);
				this.displayNewScreen(al);
			}
		} else if (cmnd == m_backCmd) {
			// Get back on mainscreen
			back();
		} else if (cmnd == m_helpCmd) {
			HelpScreen hs = new HelpScreen();
			hs.addCommand(m_backCmd);
			hs.setCommandListener(this);
			m_display.setCurrent(hs);
		} else if (cmnd == m_aboutCmd) {
			AboutScreen as = new AboutScreen(this, m_mainScreen);
			as.setCommandListener(as);
			m_display.setCurrent(as);
		} else if (cmnd == m_playCmd) {
			play();
		} else if (cmnd == m_pauseCmd) {
			pauseSong();
		} else if (cmnd == m_selectCmd) {
			TrackSelecter ts = new TrackSelecter(this, m_mainScreen);
			ts.setCommandListener(ts);
			m_display.setCurrent(ts);
		}
	}

	/**
	 * Method displays homescreen on the device
	 */
	public void back() {
		m_display.setCurrent(m_mainScreen);
		m_mainScreen.repaint();
	}

	/**
	 * Method display a new screen on the device
	 * @param d 
	 */
	public void displayNewScreen(Displayable d) {
		if (m_display != null) {
			m_display.setCurrent(m_mainScreen);
			m_display.setCurrent(d);
			if (d instanceof Canvas || d instanceof GameCanvas) {
				((Canvas) d).repaint();
			}
		}
	}

	/**
	 * Method plays the current song in the tracklist
	 */
	public void play() {
		Thread t = new Thread(m_player);
		t.start();
		m_mainScreen.removeCommand(m_playCmd);
		m_mainScreen.addCommand(m_pauseCmd);
		m_mainScreen.setCommandListener(this);
		m_mainScreen.repaint();
	}

	/**
	 * Method plays the specific song
	 * @param name 
	 */
	public void play(String name) {
		TrackSelecter.setCURRENT_TRACK(name);
		play();
	}

	/**
	 * Method stops the thread that is currently playing a song
	 */
	public void pauseSong() {
		if (m_player != null) {
			m_player.pausePlayer();
			m_mainScreen.removeCommand(m_pauseCmd);
			m_mainScreen.addCommand(m_playCmd);
			m_mainScreen.setCommandListener(this);
			m_mainScreen.repaint();
		}
	}

	/**
	 * Method stops all thread that could possibly be running
	 */
	public void stopThreads() {
		pauseSong();
		nc.stop();
	}
	
}
